#ifndef MODULES_CORE_RANDOM_H_
#define MODULES_CORE_RANDOM_H_
#include "../common.h"

template<class T>
struct RandomRatio {
public:
	T content; //内容
	uint32_t ratio; //0-100

	bool isHit(uint32_t max = 101) {
		uint32_t ret = random(max);
		// ret <=ratio
		// ret >preConfig
		return ret <= this->ratio && ret > 0;
	}
};
template<class T>
static T random(std::vector<RandomRatio<T>> &ratios, size_t defaultConfig = -1) {
	const size_t len = ratios.size();

	uint32_t preS = 0;
	uint32_t preE = 0;
	uint32_t sum = 0;

	for (auto rr : ratios) {
		sum += rr.ratio;
	}
	uint32_t ret = random(sum + 1);

	for (auto rr : ratios) {
		preE += rr.ratio;
		if (ret <= preE && ret > preS) {
			return rr.content;
		}
		preS += rr.ratio;
	}
	if (defaultConfig < 0 || defaultConfig >= len) {
		defaultConfig = len - 1;
	}
	return ratios[defaultConfig].content;
}
#endif /* MODULES_CORE_RANDOM_H_ */
